﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.BusinessRules.View;
using Basic4Net.Entities;
using Microsoft.Data.Extensions;
using System.Data.Common;
using System.Data.Objects;

namespace Basic4Net.BusinessRules
{
    public class MeritPay
    {

        private static MeritPay _meritPayInstance;
        public static MeritPay MeritPayInstance
        {
            get { return _meritPayInstance ?? (_meritPayInstance = new MeritPay()); }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="startAtt"></param>
        /// <param name="step"></param>
        /// <param name="schoolMark"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="teachCode"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<t_MeritPay> FindAll(string schoolMark,int year,int month,string teachCode)
        {
            using (DBEntities db = new DBEntities())
            {
                var result = from b in db.t_MeritPay
                             where b.SchoolMark == schoolMark && b.FYear == year && b.FMonth == month
                                   && teachCode == null ? true
                                       : (b.TeachCode.Contains(teachCode) || b.TeachName.Contains(teachCode))
                             orderby b.TeachCode
                             select b;
               
                IList<t_MeritPay> list = result.ToList<t_MeritPay>();
                return list;
            }
        }


        public List<TeachVacation> GetHolidayData(int startAt, int step, string schoolMark, int year, int month, string arrTeachId, string teachCode, ref int count)
        {
            using (DBEntities db = new DBEntities())
            {
                List<TeachVacation> list = new List<TeachVacation>();

                var result = from b in db.t_MeritPay
                             let id = b.TeacherID.ToString()
                             where b.SchoolMark == schoolMark && b.FYear == year && b.FMonth == month
                             && arrTeachId==null?true:arrTeachId.Contains(id)
                             && teachCode==null?true:(b.TeachCode.Contains(teachCode) || b.TeachName.Contains(teachCode))
                             orderby b.TeachCode
                             select new TeachVacation
                                        {
                                            TeacherID = b.TeacherID.HasValue?(int)b.TeacherID:0,
                                            TeachCode = b.TeachCode,
                                            TeachName = b.TeachName,
                                            HolidayWork = b.HolidayWork.HasValue?(double)b.HolidayWork:0,
                                            HolidayWorkContent = b.HolidayWorkContent 
                                        };
                string s = result.ToTraceString();
                count = result.Count();
                list = result.Skip(startAt).Take(step).ToList<TeachVacation>();
              //  list = result.ToList<object>();
                return list;
            } 
        }


        /// <summary>
        /// 根据教职工类别查询出其唯一的年月
        /// </summary>
        /// <param name="schoolMark">学校标识</param>
        /// <param name="iTeacherType">教工类别</param>
        /// <returns></returns>
        public IList<string> GetMeritPayMonth(string schoolMark, int iTeacherType)
        {
            DBEntities db;
            using (db = new DBEntities())
            {
                var list = (from tM in db.t_MeritPay
                            where tM.SchoolMark == schoolMark && tM.TeachType == iTeacherType
                            orderby tM.FYear, tM.FMonth ascending
                            select new
                            {
                                Year = tM.FYear,
                                Month = tM.FMonth
                            }).Distinct();

                var res = list.ToList();
                IList<string> dtList = new List<string>();
                foreach (var re in res)
                {
                    dtList.Add(string.Format("{0}-{1}", re.Year, re.Month));
                }
                return dtList;
            }
        }

        /// <summary>
        /// 按条件查询绩效工资信息
        /// </summary>
        /// <param name="Year">年</param>
        /// <param name="Month">月</param>
        /// <param name="TeachType">教工类别</param>
        /// <param name="isearchType">查询类型，0-编号，1-姓名</param>
        /// <param name="strKey">查询关键字</param>
        /// <returns></returns>
        public List<DbDataRecord> GetTeacherMeritPayList(int iYear, int iMonth, int iTeachType, int isearchType, string strKey, bool bIsSuper, int teachID)
        {
            using (DBEntities db = new DBEntities())
            {  
                string strSQL= "";
                if (!bIsSuper)
                {
                    strSQL = String.Format(" select t.rid as TeacherID, t.teachName as TeacherName,t.teachCode as TeacherCode,     tm.AmountIssued as AmountIssued,(case when tm.AmountIssued == 800 then '一等' when tm.AmountIssued == 600 then '二等' when tm.AmountIssued == 400 then '三等' else 'NULL' end) as MeritPayLevel from DBEntities.t_MeritPay as tm right join DBEntities.teacherinfo as t on tm.TeacherID = t.rid where t.rid = {0} and tm.TeachType = {1} and tm.FYear = {2} and tm.FMonth = {3}", teachID, iTeachType, iYear, iMonth);
                }
                else
                {
                    if (isearchType == 0)
                    {
                        strSQL = String.Format(" select t.rid as TeacherID, t.teachName as TeacherName,t.teachCode as TeacherCode,     tm.AmountIssued as AmountIssued,(case when tm.AmountIssued == 800 then '一等' when tm.AmountIssued == 600 then '二等' when tm.AmountIssued == 400 then '三等' else 'NULL' end) as MeritPayLevel from DBEntities.t_MeritPay as tm right join DBEntities.teacherinfo as t on tm.TeacherID = t.rid where t.teachcode like '%{0}%' and tm.TeachType = {1} and tm.FYear = {2} and tm.FMonth = {3}", strKey, iTeachType, iYear, iMonth);
                    }
                    else
                    {
                        strSQL = String.Format("select t.rid as TeacherID, t.teachName as TeacherName,t.teachCode as TeacherCode,     tm.AmountIssued as AmountIssued,(case when tm.AmountIssued == 800 then '一等' when tm.AmountIssued == 600 then '二等' when tm.AmountIssued == 400 then '三等' else 'NULL' end) as MeritPayLevel from DBEntities.t_MeritPay as tm right join DBEntities.teacherinfo as t on tm.TeacherID = t.rid where t.teachname like '%{0}%' and tm.TeachType = {1} and tm.FYear = {2} and tm.FMonth = {3}", strKey, iTeachType, iYear, iMonth);
                    }
                }
                ObjectQuery<DbDataRecord> query = db.CreateQuery<DbDataRecord>(strSQL);

                return query.ToList();
            }
        }       
    }
}
